package com.atguigu.service_edu.service.impl;

import com.atguigu.commonutils.R;
import com.atguigu.service_edu.client.VodClient;
import com.atguigu.service_edu.entity.EduVideo;
import com.atguigu.service_edu.mapper.EduVideoMapper;
import com.atguigu.service_edu.service.EduVideoService;
import com.atguigu.servicebase.exception.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.management.Query;
import java.util.List;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author clark
 * @since 2020-04-10
 */
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements EduVideoService {

    @Autowired
    private VodClient vodClient;

    @Override
    public void removeByCourseId(String courseId) {
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        baseMapper.delete(wrapper);


    }

    @Override
    public void removeVideo(String id) {
        EduVideo eduVideo = baseMapper.selectById(id);
        if(!StringUtils.isEmpty(eduVideo.getVideoSourceId())){
            R r = vodClient.deleteAlyiVideo(eduVideo.getVideoSourceId());
            if (r.getCode() == 20001){
                throw new GuliException(20001,"视频删除错误");

            }
        }
        baseMapper.deleteById(id);
    }

    @Override
    public List<EduVideo> getVideosByCourseId(String courseId) {
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        List<EduVideo> eduVideos = baseMapper.selectList(wrapper);
        return eduVideos;
    }


}
